package com.yhx.security.gfoBank.api.com.test;
import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;


public class MD5 {

    public static String getMD5(String fileName) throws Exception {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            String path= MD5.class.getClassLoader().getResource("").getPath();
           // File file=new File(path+fileName);
            File file=new File("D:\\Coding\\threadStudy\\src\\main\\webapp\\eleme001.puk");
            FileInputStream fi=new FileInputStream(file);
            byte[] buffer=new byte[8192];
            int length;
            while((length=fi.read(buffer))!=-1) {
                md5.update(buffer, 0, length);
            }
            byte tmp[] = md5.digest();
            return MD5.byte2hex(tmp);
        }catch(Exception e) {
            throw e;
        }
    }

    public static String getStringMD5(String str) throws Exception {
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");

            String path= MD5.class.getClassLoader().getResource("").getPath();
            md5.update(str.getBytes());

            byte tmp[] = md5.digest();
            return MD5.byte2hex(tmp);
        }catch(Exception e) {
            throw e;
        }
    }

    public static String byte2hex(byte[] b) {
        StringBuffer hs = new StringBuffer();
        String stmp;
        for (int i = 0; i < b.length; i++) {
            stmp = Integer.toHexString(b[i] & 0xFF).toUpperCase();
            if (stmp.length() == 1) {
                hs.append("0").append(stmp);
            } else {
                hs.append(stmp);
            }
        }
        return hs.toString();
    }

    public static void main(String[] args) {
        double d=8192/1024;
        System.out.println(d);
    }
}
